package com.meiyou.utils.jedis;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.meiyou.pojo.Coordinate;
import com.meiyou.pojo.Coordinates;

import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.GeoRadiusParam;

public class JedisClientPool implements JedisClient {
	
	@Autowired
	private JedisPool jedisPool;

	public JedisPool getJedisPool() {
		return jedisPool;
	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}

	
	public String set(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.set(key, value);
		jedis.close();
		return result;
	}

	
	public String get(String key) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.get(key);
		jedis.close();
		return result;
	}

	
	public Boolean exists(String key) {
		Jedis jedis = jedisPool.getResource();
		Boolean result = jedis.exists(key);
		jedis.close();
		return result;
	}

	
	public Long expire(String key, int seconds) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.expire(key, seconds);
		jedis.close();
		return result;
	}

	
	public Long ttl(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.ttl(key);
		jedis.close();
		return result;
	}

	
	public Long incr(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.incr(key);
		jedis.close();
		return result;
	}

	
	public Long hset(String key, String field, String value) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.hset(key, field, value);
		jedis.close();
		return result;
	}

	
	public String hget(String key, String field) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.hget(key, field);
		jedis.close();
		return result;
	}

	
	public Long hdel(String key, String... field) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.hdel(key, field);
		jedis.close();
		return result;	
	}

	
	public Long sadd(String key, String... members) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.sadd(key, members);
		jedis.close();
		return result;
	}
	
	
	public Long scard(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.scard(key);
		jedis.close();
		return result;
	}
	
	
	public Boolean sismember(String key,String member) {
		Jedis jedis = jedisPool.getResource();
		Boolean result = jedis.sismember(key, member);
		jedis.close();
		return result;
	}

	
	public Long srem(String key, String member) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.srem(key, member);
		jedis.close();
		return result;
	}

	
	public void setValueTime(String key, String value, Integer seconds) {
		
		Jedis jedis = jedisPool.getResource();
		jedis.set(key, value);
		jedis.expire(key, seconds);
		jedis.close();
	}

	
	public void del(String key) {
		
		Jedis jedis = jedisPool.getResource();
		jedis.del(key);
		jedis.close();
	}

	
	public Long addReo(Coordinate coordinate, String table) {
		// TODO Auto-generated method stub
		Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            //第一个参数可以理解为表名  
            return jedis.geoadd(table,coordinate.getLongitude(),coordinate.getLatitude(),coordinate.getKey());  
        } catch (Exception e) {  
            System.out.println(e.getMessage());  
        } finally {  
            if (null != jedis)  
                jedis.close();  
        }  
        return null;
	}
	
	
	public List<GeoRadiusResponse> geoQuery(Coordinate coordinate,int fanwei,String table) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //200F GeoUnit.KM表示km 
            return jedis.georadius(table,coordinate.getLongitude(),coordinate.getLatitude(),fanwei,GeoUnit.KM, GeoRadiusParam.geoRadiusParam().withDist());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            if (null != jedis)
                jedis.close();
        }
        return null;
    }

}
